package borge.web.teamspirit.client.servercomm;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import borge.web.teamspirit.client.interfaces.PersistentData;
import borge.web.teamspirit.client.interfaces.ServerCallbackHandler;
import borge.web.teamspirit.client.interfaces.PersistentData.DataKey;
import borge.web.teamspirit.client.interfaces.ServerCallbackHandler.CallbackType;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class ServerComm {

    private static final DataServiceAsync sDataService = GWT.create(DataService.class);
    
    private ServerComm(){ }
    

    public static void loginUser(String aUserName, String aPassword, ServerCallbackHandler aCallbackHandler){
    	
    	final ServerCallbackHandler callbackHandler = aCallbackHandler;
        sDataService.loginUser(aUserName, aPassword,
                new AsyncCallback<DBQueryResults>() {
                    public void onFailure(Throwable caught) {
                        callbackHandler.handleServerError(caught);
                    }

                    public void onSuccess(DBQueryResults result) {
                        callbackHandler.handleServerCallback(result, CallbackType.USER_LOGGED_IN);
                    }
                });
        
    	
    }
    
    public static void destroyUserSession(String aId, ServerCallbackHandler aCallbackHandler){
    	
    	final ServerCallbackHandler callbackHandler = aCallbackHandler;
        sDataService.destroyUserSession(aId, 
                new AsyncCallback<DBQueryResults>() {
                    public void onFailure(Throwable caught) {
                        callbackHandler.handleServerError(caught);
                    }

                    public void onSuccess(DBQueryResults result) {
                        callbackHandler.handleServerCallback(result, CallbackType.USER_LOGGED_OUT);
                    }
                });
        
    	
    }
    
    public static void isSessionValid(String aId, ServerCallbackHandler aCallbackHandler){
    	
    	final ServerCallbackHandler callbackHandler = aCallbackHandler;
        sDataService.isSessionValid(aId,
                new AsyncCallback<DBQueryResults>() {
                    public void onFailure(Throwable caught) {
                        callbackHandler.handleServerError(caught);
                    }

                    public void onSuccess(DBQueryResults result) {
                        callbackHandler.handleServerCallback(result, CallbackType.SESSION_VALIDITY);
                    }
                });
        
    	
    }
    
    public static void getSessionValue(String aKey, ServerCallbackHandler aCallbackHandler){
    	
    	final ServerCallbackHandler callbackHandler = aCallbackHandler;
        sDataService.getSessionValue(aKey,
                new AsyncCallback<DBQueryResults>() {
                    public void onFailure(Throwable caught) {
                        callbackHandler.handleServerError(caught);
                    }

                    public void onSuccess(DBQueryResults result) {
                        callbackHandler.handleServerCallback(result, CallbackType.DELETED);
                    }
                });
        
    	
    }
//    
//    public static void deletePlayer(TeamPlayer aPlayer, ServerCallbackHandler aCallbackHandler){
//        final ServerCallbackHandler callbackHandler = aCallbackHandler;
//        sDataService.deletePlayer(aPlayer,
//                new AsyncCallback<DBQueryResult>() {
//                    public void onFailure(Throwable caught) {
//                        callbackHandler.handleServerError(caught);
//                    }
//
//                    public void onSuccess(DBQueryResult result) {
//                        callbackHandler.handleServerCallback(result, CallbackType.DELETED);
//                    }
//                });
//        
//    }
//    
//    public static void deleteSerie(Serie aSerie, ServerCallbackHandler aCallbackHandler){
//        final ServerCallbackHandler callbackHandler = aCallbackHandler;
//        sDataService.deleteSerie(aSerie,
//                new AsyncCallback<DBQueryResult>() {
//                    public void onFailure(Throwable caught) {
//                        callbackHandler.handleServerError(caught);
//                    }
//
//                    public void onSuccess(DBQueryResult result) {
//                        callbackHandler.handleServerCallback(result, CallbackType.DELETED);
//                    }
//                });
//        
//    }
//    
//    public static void deleteMatch(Match aMatch, ServerCallbackHandler aCallbackHandler){
//        final ServerCallbackHandler callbackHandler = aCallbackHandler;
//        sDataService.deleteMatch(aMatch,
//                new AsyncCallback<DBQueryResult>() {
//                    public void onFailure(Throwable caught) {
//                        callbackHandler.handleServerError(caught);
//                    }
//
//                    public void onSuccess(DBQueryResult result) {
//                        callbackHandler.handleServerCallback(result, CallbackType.DELETED);
//                    }
//                });
//        
//    }
//    
//
//    
//    public static void getPlayerList(DBQuery aQuery, ServerCallbackHandler aCallbackHandler){
//        
//        final ServerCallbackHandler callbackHandler = aCallbackHandler;
//        sDataService.getObjectList(aQuery,
//                new AsyncCallback<DBQueryResult>() {
//                    public void onFailure(Throwable caught) {
//                        callbackHandler.handleServerError(caught);
//                    }
//
//                    public void onSuccess(DBQueryResult result) {
//                        callbackHandler.handleServerCallback(result, CallbackType.RETRIEVED_DATA);
//                    }
//                });
//        
//        
//    }
    
    
    public static <T extends PersistentData> void saveObject(T aObject, ServerCallbackHandler aCallbackHandler){
        
        final ServerCallbackHandler callbackHandler = aCallbackHandler;
        sDataService.saveObject(aObject,
                new AsyncCallback<DBQueryResults>() {
                    public void onFailure(Throwable caught) {
                        callbackHandler.handleServerError(caught);
                    }

                    public void onSuccess(DBQueryResults result) {
                        callbackHandler.handleServerCallback(result, CallbackType.CREATED);
                    }
                });
        
    }

    public static <T extends PersistentData> void deleteObject(T aObject, ServerCallbackHandler aCallbackHandler){
        
    	DBQuery query = new DBQuery(DataKey.NO_KEY, DBQuery.DELETE, aObject.getClass().getName());
    	query.addFilter("iEncodedKey", DBQuery.EQUALS, "keyParam", "String", aObject.getEncodedKey(), null);
    	
        final ServerCallbackHandler callbackHandler = aCallbackHandler;
        sDataService.deleteObject(query,
                new AsyncCallback<DBQueryResults>() {
                    public void onFailure(Throwable caught) {
                        callbackHandler.handleServerError(caught);
                    }

                    public void onSuccess(DBQueryResults result) {
                        callbackHandler.handleServerCallback(result, CallbackType.DELETED);
                    }
                });
        
    }

    
    public static <T extends Serializable> void isFieldUnique(DBQuery aQuery, ServerCallbackHandler aCallbackHandler){
        
    	//Window.alert("getobjectlist");
        final ServerCallbackHandler callbackHandler = aCallbackHandler;
        sDataService.isFieldUnique(aQuery, 
                new AsyncCallback<DBQueryResults>() {
                    public void onFailure(Throwable caught) {
                        callbackHandler.handleServerError(caught);
                    }

                    public void onSuccess(DBQueryResults result) {
                        callbackHandler.handleServerCallback(result, CallbackType.UNIQUE_DATA);
                    }
                });
        
    }

    
    public static <T extends PersistentData> void getObjectList(DBQuery aQuery, ServerCallbackHandler aCallbackHandler){
        
    	//Window.alert("getobjectlist");
        final ServerCallbackHandler callbackHandler = aCallbackHandler;
        sDataService.getObjectList(aQuery,
                new AsyncCallback<DBQueryResults>() {
                    public void onFailure(Throwable caught) {
                        callbackHandler.handleServerError(caught);
                    }

                    public void onSuccess(DBQueryResults result) {
                        callbackHandler.handleServerCallback(result, CallbackType.RETRIEVED_DATA);
                    }
                });
        
    }
    
    public static void getObjectChildren(ArrayList<DBQuery> aQueries, ServerCallbackHandler aCallbackHandler){
        
    	//Window.alert("getobjectlist");
        final ServerCallbackHandler callbackHandler = aCallbackHandler;
        sDataService.getObjectChildren(aQueries,
                new AsyncCallback<DBQueryResults>() {
                    public void onFailure(Throwable caught) {
                        callbackHandler.handleServerError(caught);
                    }

                    public void onSuccess(DBQueryResults result) {
                        callbackHandler.handleServerCallback(result, CallbackType.RETRIEVED_DATA);
                    }
                });
        
    }

    public static <T extends PersistentData> void getAllSerieData(DBQuery aQuery, ServerCallbackHandler aCallbackHandler){
        
    	//Window.alert("getobjectlist");
        final ServerCallbackHandler callbackHandler = aCallbackHandler;
        sDataService.getAllSerieData(aQuery,
                new AsyncCallback<DBQueryResults>() {
                    public void onFailure(Throwable caught) {
                        callbackHandler.handleServerError(caught);
                    }

                    public void onSuccess(DBQueryResults result) {
                        callbackHandler.handleServerCallback(result, CallbackType.RETRIEVED_DATA);
                    }
                });
        
    }


    public static <T extends PersistentData> void getSerieStats(DBQuery aQuery, ServerCallbackHandler aCallbackHandler){
        
    	//Window.alert("getobjectlist");
        final ServerCallbackHandler callbackHandler = aCallbackHandler;
        sDataService.getSerieStats(aQuery,
                new AsyncCallback<DBQueryResults>() {
                    public void onFailure(Throwable caught) {
                        callbackHandler.handleServerError(caught);
                    }

                    public void onSuccess(DBQueryResults result) {
                        callbackHandler.handleServerCallback(result, CallbackType.RETRIEVED_DATA);
                    }
                });
        
    }
    
    public static <T extends PersistentData> void getAllSeriesStats(List<DBQuery> aQueries, ServerCallbackHandler aCallbackHandler){
        
    	//Window.alert("getobjectlist");
        final ServerCallbackHandler callbackHandler = aCallbackHandler;
        sDataService.getAllSeriesStats(aQueries,
                new AsyncCallback<DBQueryResults>() {
                    public void onFailure(Throwable caught) {
                        callbackHandler.handleServerError(caught);
                    }

                    public void onSuccess(DBQueryResults result) {
                        callbackHandler.handleServerCallback(result, CallbackType.RETRIEVED_DATA);
                    }
                });
        
    }


//    public static void savePlayer(TeamPlayer aPlayer, ServerCallbackHandler aCallbackHandler){
//        final ServerCallbackHandler callbackHandler = aCallbackHandler;
//        sDataService.savePlayer(aPlayer,
//                new AsyncCallback<DBQueryResult>() {
//                    public void onFailure(Throwable caught) {
//                        callbackHandler.handleServerError(caught);
//                    }
//
//                    public void onSuccess(DBQueryResult result) {
//                        callbackHandler.handleServerCallback(result, CallbackType.CREATED);
//                    }
//                });
//        
//    }
//    
//    public static void saveSerie(Serie aSerie, ServerCallbackHandler aCallbackHandler){
//        final ServerCallbackHandler callbackHandler = aCallbackHandler;
//        sDataService.saveSerie(aSerie,
//                new AsyncCallback<DBQueryResult>() {
//                    public void onFailure(Throwable caught) {
//                        callbackHandler.handleServerError(caught);
//                    }
//
//                    public void onSuccess(DBQueryResult result) {
//                        callbackHandler.handleServerCallback(result, CallbackType.CREATED);
//                    }
//                });
//        
//    }
//    
//    public static void saveMatch(Match aMatch, ServerCallbackHandler aCallbackHandler){
//        final ServerCallbackHandler callbackHandler = aCallbackHandler;
//        sDataService.saveMatch(aMatch,
//                new AsyncCallback<DBQueryResult>() {
//                    public void onFailure(Throwable caught) {
//                        callbackHandler.handleServerError(caught);
//                    }
//
//                    public void onSuccess(DBQueryResult result) {
//                        callbackHandler.handleServerCallback(result, CallbackType.CREATED);
//                    }
//                });
//        
//    }
    
    public static void testCreate(){
    	sDataService.testCreateEntities(new AsyncCallback<DBQueryResults>() {
            public void onFailure(Throwable caught) {
           //     callbackHandler.handleServerError(caught);
            }

            public void onSuccess(DBQueryResults result) {
             //   callbackHandler.handleServerCallback(result, CallbackType.CREATED);
            }
        });
    }
 
    public static void testPrint(){
    	sDataService.testPrintEntities(new AsyncCallback<DBQueryResults>() {
            public void onFailure(Throwable caught) {
           //     callbackHandler.handleServerError(caught);
            }

            public void onSuccess(DBQueryResults result) {
             //   callbackHandler.handleServerCallback(result, CallbackType.CREATED);
            }
        });
    }

    
    
    /*
    public void savePlayer(Player aPlayer, ServerCallbackHandler aCallbackHandler){
        final ServerCallbackHandler callbackHandler = aCallbackHandler;
        iDataService.savePlayer(aPlayer,
                new AsyncCallback<DBQueryResult>() {
                    public void onFailure(Throwable caught) {
                        callbackHandler.handleServerError(caught);
                    }

                    public void onSuccess(DBQueryResult result) {
                        callbackHandler.handleServerCallback(result, CallbackType.CREATED);
                    }
                });
        
    }*/

    
    
}
